﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Xml;
using System.Collections.ObjectModel;
using Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;
using System.Drawing;
using Server.Business;
using Server.Resource;

namespace Server.Business
{
    public abstract class AbstractGenerator
    {
        protected const string FacadeXpath = "/灯具的相关表/基本信息/外观";
        protected const string SizeXpath = "/灯具的相关表/基本信息/尺寸";
        protected const string GradingXpath = "/灯具的相关表/基本信息/配光";
        protected const string LiveXpath = "/灯具的相关表/基本信息/实景";
        protected const string ListMapName = "灯具清单";
        protected const string SpecMapName = "SPEC完全版打印";

        protected abstract string TemplateName { get; }

        protected void MappingRange(Excel.Worksheet sheet, string xpath, Excel.XmlMap map, string imageUrl)
        {
            Excel.Range range = sheet.XmlMapQuery(xpath, Type.Missing, map);
            if (range != null)
                SetWorksheetImage(sheet, range, imageUrl);
        }

        protected void SetWorksheetImage(Excel.Worksheet sheet, Excel.Range rngImage, string imageUrl)
        {
            Excel.Range imageRange = rngImage.MergeArea;

            float cellWidth = (float)Convert.ToDouble(imageRange.Width) - 4;
            float cellHeight = (float)Convert.ToDouble(imageRange.Height) - 4;

            Image image = Image.FromFile(imageUrl);
            Size size = image.Size;

            float imageWidth = (float)image.Width;
            float imageHeight = (float)image.Height;

            float actualWidth = 0;
            float actualHeight = 0;

            if (cellWidth >= imageWidth)
            {
                if (cellHeight >= imageHeight)
                {
                    actualWidth = imageWidth;
                    actualHeight = imageHeight;
                }
                else
                {
                    actualHeight = cellHeight;
                    actualWidth = cellHeight * imageWidth / imageHeight;
                }
            }
            else
            {
                if (cellHeight >= imageHeight)
                {
                    actualWidth = cellWidth;
                    actualHeight = imageHeight * cellWidth / imageWidth;
                }
                else
                {
                    float zoomOutWidth = imageWidth / cellWidth;
                    float zoomOutHeight = imageHeight / cellHeight;
                    if (zoomOutWidth >= zoomOutHeight)
                    {
                        actualWidth = cellWidth;
                        actualHeight = imageHeight * cellWidth / imageWidth;
                    }
                    else
                    {
                        actualHeight = cellHeight;
                        actualWidth = cellHeight * imageWidth / imageHeight;
                    }
                }
            }
            sheet.Shapes.AddPicture(imageUrl,
                MsoTriState.msoFalse,
                MsoTriState.msoTrue,
                (float)Convert.ToDouble(imageRange.Left) + 2,
                (float)Convert.ToDouble(imageRange.Top) + 2,
                (float)Convert.ToDouble(actualWidth),
                (float)Convert.ToDouble(actualHeight));
        }

        protected Excel.XmlMap SearchMap(Excel.Workbook wb, string mapName)
        {
            foreach (Excel.XmlMap xmlMap in wb.XmlMaps)
            {
                if (xmlMap.Name == mapName)
                    return xmlMap;
            }
            return null;
        }

        //protected string CopyTemplate()
        //{
        //    string sourceFile = Path.Combine(LightingServerContext.TemplateRoot, TemplateName + ".xlsx");
        //    string targetFile = Path.Combine(LightingServerContext.ExportRoot, TemplateName + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx");
        //    File.Copy(sourceFile, targetFile, true);
        //    return targetFile;
        //}

        protected string GetPDFPath()
        {
            return Path.Combine(ServerConfig.Singleton.ExportRoot, TemplateName + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf");
        }


        [DllImport("User32.dll", CharSet = CharSet.Auto)]
        public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);

        public static void Kill(Excel.Application excel)
        {
            IntPtr t = new IntPtr(excel.Hwnd);   //得到这个句柄，具体作用是得到这块内存入口   

            int k = 0;
            GetWindowThreadProcessId(t, out k);   //得到本进程唯一标志k  
            System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);   //得到对进程k的引用  
            p.Kill();     //关闭进程k  
        }
    }
}
